package com.three.service.impl;

import com.three.entity.BranchGoods;
import com.three.entity.Outlibrary;
import com.three.entity.Staff;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.GoodsMapper;
import com.three.mapper.OutlibraryGoodsMapper;
import com.three.mapper.OutlibraryMapper;
import com.three.service.AddOutService;
import com.three.vo.OutGoodsListVo;
import com.three.vo.OutGoodsVo;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AddOutServiceImpl implements AddOutService {
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;
    @Autowired
    private OutlibraryMapper outlibraryMapper;
    @Autowired
    private OutlibraryGoodsMapper outlibraryGoodsMapper;
    @Autowired
    private HttpSession session;

    @Override
    //零售
    public R retail(BranchGoods branchGoods) {
        //根据id查询商品的店铺id
        BranchGoods branchGoods1 = branchGoodsMapper.selectByPrimaryKey(branchGoods.getId());
        //判断登陆者的店铺id与要进行零售的店铺id是否一致
        Staff staff = (Staff) session.getAttribute("staff");
        //如果一致则可以零售
        if (staff.getBranchid().equals(branchGoods1.getBranchid())) {
            int i = branchGoodsMapper.updateByPrimaryKeySelective(branchGoods);
            if (i > 0) {
                return R.ok("零售成功");
            } else {
                return R.error("零售失败");
            }
        } else {
            return R.error("权限不够");
        }
    }

    @Override
    public R measured(ArrayList<OutGoodsListVo> list) {
        //获取登陆者
        Staff staff = (Staff) session.getAttribute("staff");
        //对outlibrary进行添加操作 ,创建一个outlibrary对象
        Outlibrary outlibrary = new Outlibrary();
        outlibrary.setOutstaffid(staff.getStaffid());//添加出库人员
        outlibrary.setOuttime(new Date());//添加出库时间
        outlibrary.setOutmethod(2);//添加出库方式
        outlibrary.setExamstate(1);//添加审核状态
        outlibrary.setBranchid(staff.getBranchid());//添加店铺id
        int i = outlibraryMapper.insertSelective(outlibrary);
        if (i > 0) {
            for (OutGoodsListVo outGoodsListVo : list) {
                //为outGoodsVo赋值，添加outGoods
                OutGoodsVo outGoodsVo = new OutGoodsVo();
                outGoodsVo.setCid(outlibrary.getCid());
                outGoodsVo.setGoodsid(outGoodsListVo.getGoodsid());
                outGoodsVo.setNum(outGoodsListVo.getChuku());
                outGoodsVo.setSprice(outGoodsListVo.getSprice());
                outGoodsVo.setTotal(outGoodsListVo.getSprice()*outGoodsListVo.getChuku());
                //添加商品
                int j = outlibraryGoodsMapper.insertOutGoods(outGoodsVo);
            }
        }
        return R.ok();
    }


}
